<?php
namespace App\Http\Controllers\Member;

use Illuminate\Http\Request;
use App\Models\UserReport;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
/**
 *
 * @author Joker
 *        
 */
class ReportController extends MemberController
{
    protected $report_type = array();
    protected $report_type_2 = array();
    
    public function __construct()
    {
        parent::__construct();
        $this->report_type = array(1=>'个人攻击',2=>'企业诽谤');
        $this->report_type_2 = array(1=>'广告类',2=>'政治有害类',3=>'暴恐类',4=>'淫秽色情类',5=>'头像、签名档',6=>'赌博类',7=>'私服外挂类',8=>'诈骗类',9=>'其他有害类');
    }
    //我的投诉 收到的投诉
    public function index()
    {
        $reports = UserReport::where(['report_uid'=>$this->uid])->paginate(15);
        return view('member.report.report')->with('reports',$reports);
    }
    
    //我的投诉 我的投诉
    public function myreport()
    {
        $reports = UserReport::where(['user_id'=>$this->uid])->paginate(15);
        
        return view('member.report.report')->with('reports',$reports);
    }
    
    /**
     * 用户举报 
     */
    public function store(Request $req)
    {
        $data = $req->only('app','app_id','report_type','report_type_2','reson_type','verfy_code','verfy_code_2','url','report_uid');
        $insert_row = array();
        
        if($data['reson_type'] == 1)
        {
            $insert_row['reson'] = $this->report_type[$data['reson_type']];
            $rules = ['verfy_code' => 'required|captcha'];
            
        }
        else 
        {
            $insert_row['reson'] =$this->report_type_2[$data['reson_type']];
            $rules = ['verfy_code_2' => 'required|captcha'];
        }
        $validate = Validator::make($data,$rules);
        if($validate->fails())
        {
            return $this->formatJson('error','验证码不正确！');
        }
        $insert_row['app'] = trim($data['app']);
        $insert_row['app_id'] = intval(trim($data['app_id']));
        $insert_row['url'] = trim($data['url']);
        $insert_row['user_id'] = $this->uid;
        $insert_row['report_uid'] = $data['report_uid'];
        $insert_row['status'] = 0;
        if(!$insert_row['app'] || !$insert_row['app_id'] || !$insert_row['reson'])
        {
            return $this->formatJson('error','请选择条件！');
        }
        try{
            $report = UserReport::firstOrCreate($insert_row);
        }catch (\PDOException $e){
            Log::info(['用户举报异常'=>$e->getMessage()]);
            return $this->formatJson('error','举报异常错误！');
        }
        return $this->formatJson('success','举报成功！',$report);
    }
}